<?php
namespace product\service;

use product\service\interfaces\IProductTypeService;
use common\service\CommonService;
use herosphp\core\Loader;
use herosphp\utils\ArrayUtils;

Loader::import('product.service.interfaces.IProductTypeService');

/**
 * product(Service)接口实现
 * @package product\service
 * @author yangjian<yangjian102621@gmail.com>
 */
class ProductTypeService extends CommonService implements IProductTypeService {

    public function getItem($condition, $fields, $order)
    {
        $item = parent::getItem($condition, $fields, $order);
        if ( !empty($item['pid']) ) {
            $type = $this->modelDao->getItem($item['pid']);
            $item['parent_name'] = $type['name']; //父类分类
            $item['subs_num'] = $this->modelDao->count(array('idpath' => "%,{$item['id']}%"));
        }
        return $item;
    }

    public function getItems($conditions, $fields, $order, $limit, $group, $having)
    {
        $items = parent::getItems($conditions, $fields, $order, $limit, $group, $having);
        if ( !empty($items) ) {
            $ids = array();
            foreach ( $items as $val ) {
                $ids[] = $val['pid'];
            }
            $types = $this->modelDao->getItems(array('id' => array('$in' => $ids)));
            $types = ArrayUtils::changeArrayKey($types, 'id');
            foreach ( $items as $key => $value ) {
                $items[$key]['parent_name'] = $types[$value['pid']]['name'];
                $items[$key]['subs_num'] = $this->modelDao->count(array('idpath' => "%,{$value['id']}%"));
            }
        }

        return $items;
    }
}
